package com.h3bpm.web.msg.dao.impl;

import com.h3bpm.web.common.utils.SqlUtil;
import com.h3bpm.web.msg.dao.TblSmsDao;
import data.DataRow;
import data.DataTable;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Repository
public class TblSmsDaoImpl implements TblSmsDao {
    @Override
    public List<DataRow> getNoSendSmsList() {
        try {
            String sql="select * From tbl_sms where SendFlag = '0'";
            DataTable table = SqlUtil.doQuery(sql);
            List<DataRow> entityRows = table.getEntityRows();
            return entityRows;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertSms(String number, String message) {
        try {
            String sql="insert into tbl_sms (ObjectID,Number,Message,SendFlag,CreatedTim) values (UUID(),'"+number+"','"+message+"','0',NOW())";
            int i = SqlUtil.doNoQuery(String.format(sql, number, message));
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateSms(String ObjectID) {
        try {
            String sql="update tbl_sms set SendFlag = '1' where ObjectID = '%s'";
            int i = SqlUtil.doNoQuery(String.format(sql, ObjectID));
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void clearSms() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            //过去七天
            c.setTime(new Date());
            c.add(Calendar.DATE, - 7);
            Date d = c.getTime();
            String times = format.format(d);
            String sql ="delete from tbl_sms  WHERE createdTime<'"+ times+"'";
            SqlUtil.doNoQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
